package com.ricky.game.scene;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ricky.core.net.msg.AMF3Context;
import com.ricky.game.Actor;
import com.ricky.game.Hero;
import com.ricky.game.MsgHelper;

public abstract class SceneBase {
	
	private Boolean initialize=false;
    /** 地图创建者 */
	private Hero creator;
	/**
	 * 场景控制器是否初始化
	 * @return the initialize
	 */
	public Boolean getInitialize() {
		return initialize;
	}
	
	/**
	 * 装载配置数据
	 * @param scf
	 */
	public void init()
	{
		online=new ConcurrentHashMap<String,Hero>();
		waiting=new ConcurrentHashMap<String,Hero>();
		initialize=true;
	}
	
	/**
	 * 是否能够进入场景
	 * @param hero
	 * @param sb
	 * @return
	 */
	public abstract Boolean prevEnter(Hero hero,int x,int y,StringBuilder sb);
	
	/**
	 * 添加到等待室
	 * @param hero
	 * @param x
	 * @param y
	 */
	public abstract void joinWaitingRoom(Hero hero,int x,int y,int z);
	
	/**
	 * 加入场景
	 * @param hero
	 */
	public abstract void enter(Hero hero);
	
	/**
	 * 从等候室移除
	 * @param hero
	 */
	public abstract void removeFromWaitingRoom(Hero hero);
	
	/**
	 * 从场景移出英雄
	 * @param hero
	 * @return
	 */
	public abstract void remove(Hero hero);
	
	/**
	 * 同步位置
	 * @param hero
	 */
	public abstract void synchrostep(Hero hero);
	
	/**
	 * 场景广播
	 * @param hero
	 * @param data
	 */
	public void broadcast(Hero hero,Map<String,Object> data)
	{
		Collection<Hero> actors= this.online.values();
		AMF3Context amf=new AMF3Context();
		amf.setContent(data);
		for (Actor act:actors)
		{
			if(act.getRequest()!=null && act!=hero)
				MsgHelper.sendMsg(act.getRequest(), amf);
		}
	}
	
	/**
	 * 等待室广播
	 * @param hero
	 * @param data
	 */
	public void broadcastWaitingRoom(Hero hero,Map<String,Object> data)
	{
		Collection<Hero> actors= this.waiting.values();
		AMF3Context amf=new AMF3Context();
		amf.setContent(data);
		for (Actor act:actors)
		{
			if(act.getRequest()!=null && act!=hero)//
				MsgHelper.sendMsg(act.getRequest(), amf);
		}
	}
	
	/**
	 * 场景主循环
	 */
	public abstract void update();
	
	/**
	 * @return the sceneInfo
	 */
	public Map<String,Object> getSceneInfo() {
		return sceneInfo;
	}

	/**
	 * @param sceneInfo the sceneInfo to set
	 */
	public void setSceneInfo(Map<String,Object> sceneInfo) {
		this.sceneInfo = sceneInfo;
	}


	/**
	 * @return the online
	 */
	public Map<String,Hero> getOnline() {
		return online;
	}

	/**
	 * @param online the online to set
	 */
	public void setOnline(Map<String,Hero> online) {
		this.online = online;
	}


	/**
	 * @return the waiting
	 */
	public Map<String,Hero> getWaiting() {
		return waiting;
	}

	/**
	 * @param waiting the waiting to set
	 */
	public void setWaiting(Map<String,Hero> waiting) {
		this.waiting = waiting;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the creator
	 */
	public Hero getCreator() {
		return creator;
	}

	/**
	 * @param creator the creator to set
	 */
	public void setCreator(Hero creator) {
		this.creator = creator;
	}

	/** 加载完成在地图中的玩家  */
	protected Map<String,Hero> online;
	/**  正在加载的玩家   */
	protected Map<String,Hero> waiting;
	/**  场景信息 降低耦合，由客户端确定创建的地图数据 */
	protected Map<String,Object> sceneInfo;
	/** 场景id */
	protected int id;
	/** 场景名 可以由用户自己定义 */
	protected String name;
	
}
